import { Component, OnInit, OnDestroy, ElementRef, Renderer2 } from '@angular/core';
import { FormBuilder, Validators, FormControl } from '@angular/forms';

import { ConfigService } from 'src/app/config.service';
import { GB28181Interface } from './GB28181Interface';
import { IeCssService } from 'src/app/shared/func-service/ie-css.service';
import { ResponseErrorService } from 'src/app/shared/func-service/response-error.service';
import { TipsService } from 'src/app/tips/tips.service';
import { PublicFuncService } from 'src/app/shared/func-service/public-func.service';
import { LayoutService } from 'src/app/shared/func-service/layout.service';
import Logger from 'src/app/logger';
import { isNumberJudge } from 'src/app/shared/validators/benumber.directive';
import { isPString32 } from 'src/app/shared/validators/pstring32.directive';


@Component({
  selector: 'app-gb28181',
  templateUrl: './gb28181.component.html',
  styleUrls: ['./gb28181.component.scss']
})
export class GB28181Component implements OnInit, OnDestroy  {

  constructor(
    private cfg: ConfigService,
    private fb: FormBuilder,
    private Re2: Renderer2,
    private el: ElementRef,
    private ieCss: IeCssService,
    private resError: ResponseErrorService,
    private tips: TipsService,
    private pfs: PublicFuncService,
    private los: LayoutService,
  ) { }

  private logger: Logger = new Logger('gb28181');

  isIe: boolean = true;

 
  GB28181Form = this.fb.group({
    sServerSid: ['', isPString32],
    sServerIp: ['', Validators.required],
    sServerRealm: ['', isPString32],
    iServerPort: ['', isNumberJudge],
    sIpcSip: ['', isPString32],
    sIpcPwd: ['', Validators.required]
  });

  get sServerSid(): FormControl {
    return this.GB28181Form.get('sServerSid') as FormControl;
  }
  get sServerIp(): FormControl {
    return this.GB28181Form.get('sServerIp') as FormControl;
  }
  get sServerRealm(): FormControl {
    return this.GB28181Form.get('sServerRealm') as FormControl;
  }
  get iServerPort(): FormControl {
    return this.GB28181Form.get('iServerPort') as FormControl;
  }
  get sIpcSip(): FormControl {
    return this.GB28181Form.get('sIpcSip') as FormControl;
  }
  get sIpcPwd(): FormControl {
    return this.GB28181Form.get('sIpcPwd') as FormControl;
  }
  

  ngOnInit() {
    this.isIe = this.ieCss.getIEBool();
    this.cfg.getGB28181Interface().subscribe(
      res => {
        this.resError.analyseRes(res);
        this.GB28181Form.patchValue(res);
      },
      err => {
        this.logger.error(err, 'ngOnInit:getGB28181Config:');
        this.tips.showInitFail();
      }
    );
  }

  ngOnDestroy() {
   
  }

  onSubmit() {
    this.cfg.setGB28181Interface(this.GB28181Form.value).subscribe(
      res => {
        this.resError.analyseRes(res);
        this.GB28181Form.patchValue(res);
        this.tips.showSaveSuccess();
      },
      err => {
        
        this.logger.error(err, 'onSubmit:setGB28181Interface:');
        this.tips.showSaveFail();
      }
    );
  }

  

  onTest() {
  
  }
}
